﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DiaosFix.Core
{
    public class RequestStatusOperator
    {
        private DiaosFixEntities context = new DiaosFixEntities();

        public Response<List<Requests>> GetRequestsByStatus(PubConstant.RequestStatus status)
        {
            var response = new Response<List<Requests>>();
            try
            {
                string strStatus = status.ToString();
                var q = context.RequestStatus.Where(p => p.StatusName == strStatus).SingleOrDefault();
                if (null != q)
                {
                    var requests = q.Requests;
                    if (null != requests)
                    {
                        response.IsSuccess = true;
                        response.Message = string.Empty;
                        response.Item = requests.ToList();
                    }
                    else
                    {
                        response.IsSuccess = false;
                        response.Message = "Error Fetching Request Entity Collection";
                    }
                }
                else
                {
                    response.IsSuccess = false;
                    response.Message = "Status " + strStatus + "Not Valid";
                }
            }
            catch(Exception ex)
            {
                response.Message = ex.Message;
                response.Item = null;
                response.IsSuccess = false;
            }
            return response;
        }

        public Response<List<Requests>> GetRequestsByStatus(string statusName)
        {

            var response = new Response<List<Requests>>();
            try
            {                
                var q = context.RequestStatus.Where(p => p.StatusName == statusName).SingleOrDefault();
                if (null != q)
                {
                    var requests = q.Requests;
                    if (null != requests)
                    {
                        response.IsSuccess = true;
                        response.Message = string.Empty;
                        response.Item = requests.ToList();
                    }
                    else
                    {
                        response.IsSuccess = false;
                        response.Message = "Error Fetching Request Entity Collection";
                    }
                }
                else
                {
                    response.IsSuccess = false;
                    response.Message = "Status " + statusName + "Not Valid";
                }
            }
            catch (Exception ex)
            {
                response.Message = ex.Message;
                response.Item = null;
                response.IsSuccess = false;
            }
            return response;
        }
    }
}
